<template>
  <div>
    <el-card>
      <el-table :data="skuList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="skuName" label="名称" width="80">
        </el-table-column>
        <el-table-column prop="skuDesc" label="描述" width="80">
        </el-table-column>
        <el-table-column label="默认图片" width="150" align="center">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量(KG)" width="80">
        </el-table-column>
        <el-table-column prop="price" label="价格(元)" width="80">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-tooltip class="item" content="下架" v-if="row.isSale === 1">
              <el-button
                type="success"
                class="iconfont el-icon-bottom"
                size="mini"
                @click="cancelSale(row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" content="上架" v-if="row.isSale === 0">
              <el-button
                type="info"
                class="iconfont el-icon-top"
                size="mini"
                @click="onSale(row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" content="修改">
              <el-button
                type="primary"
                class="iconfont el-icon-edit"
                size="mini"
                @click="changeSku"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" content="查看详情">
              <el-button
                type="info"
                class="iconfont el-icon-warning-outline"
                size="mini"
                @click="detail(row)"
              ></el-button>
            </el-tooltip>

            <el-popconfirm :title="`您确定要删除${row.skuName}吗？`">
              <el-button
                slot="reference"
                type="danger"
                class="iconfont el-icon-delete"
                size="mini"
                @click="del(row)"
              ></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15]"
        :page-size="limit"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        style="text-align: center"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      title="我是标题"
      :visible.sync="isShowDetail"
      :with-header="false"
      size="40%"
    >
      <el-row>
        <el-col :span="5">名称：</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述：</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格：</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性：</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            v-for="item in skuInfo.skuAttrValueList"
            :key="item.id"
            >{{ item.valueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片：</el-col>
        <el-col :span="16">
          <el-carousel height="400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" style="width: 300px; height: 400px" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 5,
      total: 20,

      skuList: [],

      isShowDetail: false,

      skuInfo: {},
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    // 初始化数据
    async getSkuList(page) {
      if (page) {
        this.page = page;
      }
      const { data } = await this.$API.sku.getList(this.page, this.limit);
      this.total = data.total;
      this.skuList = data.records;
    },
    // 上架
    async onSale(row) {
      await this.$API.sku.onSale(row.id);
      this.$message.info("上架成功！");
      // 重新获取数据，跳转到当前页
      this.getSkuList(this.page);
    },
    // 下架
    async cancelSale(row) {
      await this.$API.sku.cancelSale(row.id);

      this.$message.info("下架成功！");
      // 重新获取数据，跳转到当前页
      this.getSkuList(this.page);
    },
    // 修改
    changeSku() {
      this.$message.info("功能开发中");
    },
    // 查看详情
    async detail(row) {
      const { data } = await this.$API.sku.get(row.id);
      this.skuInfo = data;
      this.isShowDetail = true;
    },
    // 删除
    async del(row) {
      await this.$API.sku.remove(row.id);
      this.$message.info("删除成功！");
      // 重新获取数据，跳转到当前页
      this.getSkuList(this.page);
    },
    handleSizeChange(value) {
      this.limit = value;
      this.getSkuList();
    },
    handleCurrentChange(value) {
      this.page = value;
      this.getSkuList();
    },
  },
};
</script>

<style>
.item {
  margin: 4px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
